#!/usr/bin/perl
# 2014/11/10 fastqファイルを指定した長さに短くする

use strict;
use warnings;
use IO::File;
use Getopt::Std;
use Carp qw(croak);
$| = 0;

if(@ARGV != 6 or $ARGV[0] eq '--help'){
	die "Usage : $0 -i [in fastq file] -o [out fastq file] -n [length to truncate]\n";
}

my %opt;
getopts("i:o:n:", \%opt);
my $FILE_in = $opt{i};
my $FILE_out = $opt{o};
my $LENGTH_TRUNCATE = $opt{n};

my $LINE_NUM = 0;
my $fh_in = IO::File->new($FILE_in) or die "cannot open $FILE_in: $!";
my $fh_out = IO::File->new($FILE_out, 'w') or die "cannot write $FILE_out: $!";
while($_ = $fh_in->getline()){
	s/\r?\n//;
	$LINE_NUM++;
	my $T = $LINE_NUM % 4;
	if($T == 1){
		$fh_out->print("$_\n");
	}elsif($T == 2){
		my $small_seq = substr($_, 0, 25);
		$fh_out->print("$small_seq\n");
	}elsif($T == 3){
		$fh_out->print("$_\n");
	}else{
		my $small_quality = substr($_, 0, 25);
		$fh_out->print("$small_quality\n");
	}
}
$fh_in->close();
$fh_out->close();